package com.itheima.controller;


import com.itheima.anno.OperateLog;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class DeptController {
//    private static final Logger log = LoggerFactory.getLogger(DeptController.class);


    //注入DeptService
    @Autowired
    private DeptService deptService;
    //创建方法，接受请求，返回结果


    //返回所有数据
    @GetMapping("/depts")
    public Result list(){

        //调用service查询数据库
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }


    @OperateLog
    //根据Id删除数据
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        //调用service删除数据库
        deptService.deleteById(id);
        return Result.success();
    }

    @OperateLog
    //添加数据
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        deptService.add(dept);
        return Result.success();
    }

    //根据Id查询数据
    @GetMapping("/depts/{id}")
    public Result searchById(@PathVariable("id") Integer id){
        Dept dept = deptService.searchById(id);
        return Result.success(dept);
    }

    @OperateLog
    //更新数据
    @PutMapping("/depts")
    public Result updateById(@RequestBody Dept dept){
        deptService.updateById(dept);
        return Result.success();
    }

}
